package com.flycms.core.base;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.flycms.core.utils.AdminSessionUtils;
import com.flycms.module.admin.model.Admin;
import com.flycms.module.admin.service.AdminService;
import com.flycms.module.message.service.MessageService;
import com.flycms.module.other.service.FilterKeywordService;
import com.flycms.module.template.service.TemplateService;
import com.flycms.module.user.model.User;
import com.flycms.module.user.service.UserService;
import com.flycms.module.user.utils.UserSessionUtils;

/**
 * Open source house, All rights reserved
 * 开发公司：宏石<br/>
 * 版权：开源中国<br/>
 *
 *  Controller基类
 *
 * @author sun-kaifei
 * @version 1.0 <br/>
 * @email xxx@qq.com
 * @Date: 14:14 2020/7/8
 */

public class BaseController {
    @Resource
    protected HttpServletRequest request;
    @Resource
    protected HttpServletResponse response;
	@Autowired
	protected AdminService adminService;
    @Autowired
	protected UserService userService;
    @Resource
    protected HttpSession session;
	@Autowired
	protected TemplateService theme;
	
	@Autowired
	protected FilterKeywordService keywordFilter;
	
	@Autowired
    protected MessageService messageService;

	  /**
	   * 获取用户信息
	   *
	   * @return
	   */
	protected User getUser() {
		User user = UserSessionUtils.getLoginMember(request);
		if (StringUtils.isEmpty(user)) {
			return null;
		} else {
			return userService.findUserById(user.getUserId(),1);
		}
	}
	
    protected Long getUserId() {
        User user = getUser();
        return user == null ? null : user.getUserId();
    }

	/**
	 * 获取用户信息
	 *
	 * @return
	 */
	protected Admin getAdminUser() {
		Admin user = AdminSessionUtils.getLoginMember(request);
		if (StringUtils.isEmpty(user)) {
			return null;
		} else {
			Admin admin = adminService.findAdminById(user.getId(), 0);
			admin.setSessionKey(user.getSessionKey());
			admin.setRoleId(0L);
			return admin;
		}
	}
	
	protected Long getAdminUserId() {
	    Admin user = getAdminUser();
	    return user == null ? null : user.getId();
	}
}
